local mingjie = fk.CreateSkill{
  name = "wk_heg__mingjie",
  tags = {Skill.MainPlace},
}

Fk:loadTranslationTable{
  ["wk_heg__mingjie"] = "铭戒",
  [":wk_heg__mingjie"] = "主将技，此武将牌上单独的阴阳鱼个数-1；准备阶段，你可选择一名其他角色，你于本回合内使用牌可额外指定其为目标且防止对其造成的伤害。",

  ["@@wk_heg__mingjie-turn"] = "铭戒",
  ["#wk_heg__mingjie-choose"] = "铭戒：你可以选择一名其他角色，防止本回合对其造成的伤害且本回合使用牌可以额外指定其为目标",

  ["$wk_heg__mingjie1"] = "大公至正，恪忠义于国。",
  ["$wk_heg__mingjie2"] = "此生柱国之志，铭恪于胸。",
}

mingjie:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(mingjie.name) and target == player and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room:getOtherPlayers(player),
      skill_name = mingjie.name,
      prompt = "#wk_heg__mingjie-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {to = to[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to
    local mark = to:getTableMark( "@@wk_heg__mingjie-turn")
    table.insert(mark, player.id)
    room:setPlayerMark(to, "@@wk_heg__mingjie-turn", mark)
  end,
})

mingjie:addEffect(fk.AfterCardTargetDeclared, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and data.card.trueName ~= "threaten_emperor" then
      local mark
      local targets = table.filter(data:getExtraTargets(), function (p)
        mark = p:getMark("@@wk_heg__mingjie-turn")
        return type(mark) == "table" and table.contains(mark, player.id)
      end)
      if #targets > 0 then
        event:setCostData(self, {targets = targets})
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = event:getCostData(self).targets
    local tos = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = #targets,
          targets = targets,
          skill_name = mingjie.name,
          prompt = "#mingjiew-choose:::"..data.card:toLogString(),
          cancelable = true,
        })
    if #tos > 0 then
      for _, p in ipairs(tos) do
        room:doIndicate(player, {p})
        data:addTarget(p)
      end
      -- table.forEach(tos, function (p) table.insert(data.tos, {p}) end)
    end
  end,
})

mingjie:addEffect(fk.DetermineDamageCaused, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local mark = data.to:getMark("@@wk_heg__mingjie-turn")
    return player == target and type(mark) == "table" and table.contains(mark, player.id)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data:preventDamage()
  end,
})

return mingjie